#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/*
 * Load OSv .so file immediately
 */
asm(".pushsection .note.osv-mlock, \"a\"; .long 0, 0, 0; .popsection");

/*
Run this main in separate OSv ELF namespace.
*/

int global_var = 10;
extern char **environ;
int main(int argc, char **argv)
{
  int delay = 0;
  int id = -1;
  int ii;
  char **ch;

  fprintf(stderr, "PP tst-execve-payload.c main.\n");
  fprintf(stderr, "PP argc %d\n", argc);
  for (ii=0; ii<argc; ii++) {
    fprintf(stderr, "PP argv[%d] = %s\n", ii, argv[ii]);
  }
  if (argc > 1)
    id = atoi(argv[1]);
  if(argc > 2)
    delay = atoi(argv[2]);
  fprintf(stderr, "PP delay = %d us\n", delay);
  usleep(delay);

  fprintf(stderr, "PP global_var=%p %d\n", &global_var, global_var);
  global_var++;

  for (ii=0, ch=environ; ch!=NULL && *ch!=NULL && **ch!= '\0'; ii++, ch++) {
    fprintf(stderr, "PP environ[%d] = %p %s\n", ii, *ch, *ch);
  }

  usleep(delay);
  fprintf(stderr, "PP tst-execve-payload.c id=%d main EXIT\n", id);
  return(0);
}
